/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.gui;

import javax.swing.RowFilter;
import javax.swing.table.TableRowSorter;
import org.joda.time.DateTime;
import org.tnmk.worktime.gui.history.filter.TimeRowFilter;
import org.tnmk.worktime.service.filter.RangeTimeFilter;
import org.tnmk.worktime.service.filter.ThisMonthTimeFilter;
import org.tnmk.worktime.service.filter.ThisWeekTimeFilter;
import org.tnmk.worktime.utils.TimePeriodUtils;

/**
 *
 * @author tnmkhoi
 */
public class HistoryFilterController {

  private final MainFrame frame;

  public HistoryFilterController(MainFrame frame) {
    this.frame = frame;
  }

  public void filterThisWeekHistory() {
    RowFilter rowFilter = new TimeRowFilter(ThisWeekTimeFilter.getInstance());
    TableRowSorter sorter = (TableRowSorter) this.frame.getJtblHistory().getRowSorter();
    sorter.setRowFilter(rowFilter);
  }

  public void filterWeekHistory() {
    DateTime begin = (DateTime) frame.jcboWeeks.getSelectedItem();
    DateTime end = TimePeriodUtils.withTimeAtEndOfWeek(begin);
    filter(begin, end);
  }

  public void filterThisMonthHistory() {
    RowFilter rowFilter = new TimeRowFilter(ThisMonthTimeFilter.getInstance());
    TableRowSorter sorter = (TableRowSorter) this.frame.getJtblHistory().getRowSorter();
    sorter.setRowFilter(rowFilter);
  }

  public void filterMonthHistory() {
    DateTime begin = (DateTime) frame.jcboMonths.getSelectedItem();
    DateTime end = TimePeriodUtils.withTimeAtEndOfMonth(begin);
    filter(begin, end);
  }

  public void filterRangeHistory(String begin, String end, String pattern) {
    RowFilter rowFilter = new TimeRowFilter(new RangeTimeFilter(begin, end, pattern));
    TableRowSorter sorter = (TableRowSorter) this.frame.getJtblHistory().getRowSorter();
    sorter.setRowFilter(rowFilter);
  }

  private void filter(DateTime begin, DateTime end) {
    RowFilter rowFilter = new TimeRowFilter(new RangeTimeFilter(begin, end));
    TableRowSorter sorter = (TableRowSorter) this.frame.getJtblHistory().getRowSorter();
    sorter.setRowFilter(rowFilter);
  }
}
